package cc.idoone.spring_boot_hello.exception;

import cc.idoone.spring_boot_hello.exception.WebappException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 异常测试/404页面测试
 *
 * @author hemj
 * @version 2017/9/22
 */
@Controller
@RequestMapping("/error")
public class ErrorController implements org.springframework.boot.autoconfigure.web.ErrorController {

	private static final String ERROR_PATH = "/error";

	@GetMapping("/exception")
	@ResponseBody
	public String exception() throws Exception {
		throw new Exception("=== Exception ===");
	}

	@GetMapping("/illegalArgumentException")
	@ResponseBody
	public String illegalArgumentException() {
		throw new IllegalArgumentException("=== IllegalArgumentException ===");
	}

	@GetMapping("/webappException")
	@ResponseBody
	public String webappException() throws WebappException {
		throw new WebappException("=== WebappException ===");
	}

	// 自定义错误控制器
	@RequestMapping
	public String handleError(HttpServletRequest req) {
		return "error/404";
	}

	// 返回自定义错误控制器
	@Override
	public String getErrorPath() {
		return ERROR_PATH;
	}
}
